// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        rsultLabel:cc.Label,
        mask:cc.Mask,
        tNode:cc.Node,
      //  bg:cc.Node,
        retNode:cc.Node,
        itemList:{
            default:[],
            type:cc.Node,
        },
        List:[],
        isCompleted:false,
        isInited:false,

        completeCb:null,
    },

    init(pNode,cb){
        if(this.isInited || !pNode)
            return ;
        this.tNode = pNode.getChildByName("bg");
        this.retNode = pNode.getChildByName("ret");
        
       // this.rsultLabel = pNode.getChildByName("ret").getComponent(cc.Label);
        this.mask = pNode.getChildByName("maskNode").getComponent(cc.Mask);
        let itemParent = this.retNode.getChildByName('con_bg_yellow');
        let width  = this.mask.node.children[0].width - 110;
        let height = this.mask.node.children[0].height - 130;
        let widthCount = width / 50 ;
        let heightCount = height / 50;
        for(let i = 0 ; i < widthCount ;++i)
        {
          for(let k = 0 ; k < heightCount;++k)
          {
              let rect = new cc.Rect(25+i*50,25-k*50,50,50)
              this.List.push({rect:rect,isTouched:false});   
          }
        }
        cc.log(this.List);
        for(let i = 0 ; i < itemParent.childrenCount;++i)
        {
            this.itemList.push(itemParent.children[i]);
        }
        if(cb)
        {
            this.completeCb = cb;
        }
        this.reset();
        this.initEvent();
      //  this.retNode.active = true;
        this.isInited = true;
    },


    initEvent: function (){
        this.tNode.on(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
        this.tNode.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.tNode.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.tNode.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
    },

    onDestroy:function () {
        this.tNode.off(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
        this.tNode.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.tNode.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.tNode.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
    },

    _onTouchBegin(e){
        if(this.isCompleted)
            return ;
      //  this.checkAera(e);
        
        this._addCircle(e)
    },
    _onTouchMoved(e){
        if(this.isCompleted)
            return ;
        this.checkAera(e);
        this._addCircle(e)

    },
    _onTouchEnd(e){
        if(this.isCompleted)
            return ;
        this.checkAera(e);
        this._addCircle(e)

    },
    _onTouchCancel(e){
        if(this.isCompleted)
            return ;
        this.checkAera(e);
        this._addCircle(e);
    },
    
    reset(){
        //if(this.mask)
        //this.mask._refreshStencil()
        this.mask.node.active = true;
        this.retNode.active = false;
        this.tNode.active = true;
        this.isCompleted = false;
        for(let i = 0 ; i < this.List.length;++i)
        {
            let item = this.List[i];
            if(item)
            {
                item.isTouched = false;
            }
        }
        for(let i = 0 ; i < this.itemList.length;++i)
        {
            let child = this.itemList[i];
            child.getComponent(cc.Sprite).spriteFrame = null;
        }
    },

    _addCircle:function (e) {
        let point = e.touch.getLocation();
        point = this.tNode.convertToNodeSpaceAR(point);
        this.mask._graphics.lineWidth = 1;
        this.mask._graphics.strokeColor = cc.color(255,0,0);
        this.mask._graphics.fillColor = cc.color(0,255,0);
        this.mask._graphics.circle (point.x,point.y,50);
        this.mask._graphics.fill();
        this.mask._graphics.stroke();
     },

     checkAera(e){
        let point = e.touch.getLocation();
        point = this.tNode.convertToNodeSpaceAR(point);
        let rect = new cc.Rect(point.x,point.y,50,50);
        let count = 0;
        for(let i = 0 ; i < this.List.length;++i)
        {
            let item = this.List[i];
            if(item.isTouched)
            {
                count++;
                continue;
               
            }
            if(count >= Math.floor(this.List.length * 0.3))
            {
                this.isCompleted = true;
                this.mask.node.active = false;
                this.retNode.active = true;
                cc.log("刮奖完毕");
                if(this.completeCb)
                {
                    this.completeCb();
                    this.completeCb = null;
                }
                break;
            }
           let ret  = rect.intersects(item.rect);
           if(ret == true)
           {
               item.isTouched = true;
               break;
           }
        }
      //  cc.log(count);
     }
     

});
